package file.day9_20.day05.构建服务端与客户端练习;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private Socket socket;

    public Client(){
        try {
            socket = new Socket("localhost",8089);
            System.out.println("与服务器连接");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void start(){
        Scanner scanner = new Scanner(System.in);
        try(
                PrintWriter pw = new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(
                                        socket.getOutputStream(),"UTF-8"
                                )
                        ),true
                );
            ) {

            while (true){
                System.out.println("请输入内容：");
                String message = scanner.nextLine();
                if(message.equals("exit"))break;
                pw.println(message);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }

}
